﻿using System;
using System.Windows.Forms;
using QFXProxy.Model;
using QFXProxy.Presenter;


namespace QFXProxy.View
{
    public partial class MainForm : Form, IConverterView 
    {
        private readonly ConverterPresenter _presenter;

        #region Constructors
        /// <summary>
        /// Constructor.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
            _presenter = new ConverterPresenter(this, new FileConverter());
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="args"></param>
        public MainForm(string[] args) : this()
        {
            _presenter.InitializeViewData(args);
        }
        #endregion

        #region Implementation of IConverterView
        public string BankId { get; set; }

        public string SourceFile
        {
            get { return txtSourceFile.Text; }
            set { txtSourceFile.Text = value; }
        }

        /// <summary>
        /// Responds to events to convert the source file.
        /// </summary>
        public void OnConvertFile()
        {
            try
            {
                _presenter.ConvertFile();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }

        /// <summary>
        /// Force the view to close.
        /// </summary>
        public void CloseView()
        {
            Close();
        }
        #endregion

        #region Private Members
        /// <summary>
        /// Called when the form is loaded.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            OnConvertFile();
        }

        /// <summary>
        /// Responds to find source file events.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFindSourceFile_Click(object sender, EventArgs e)
        {
            openFileDialog.FileName = SourceFile;
            openFileDialog.Filter = "OFX Files|*.ofx";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                SourceFile = openFileDialog.FileName;
            }
        }

        /// <summary>
        /// Responds to on click event of the cancel button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            _presenter.OnCancel();
        }

        /// <summary>
        /// Responds to on click event of the convert button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConvert_Click(object sender, EventArgs e)
        {
            OnConvertFile();
        }

        /// <summary>
        /// Handles text changes event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtSourceFile_TextChanged(object sender, EventArgs e)
        {
            btnConvert.Enabled = (txtSourceFile.Text.Length > 0);
        }
        #endregion
    }
}